package com.demo.es.config;

import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author 李炯
 * @description: ElasticSearch 客户端配置类
 * @program: study
 * @date 2020/05/17
 **/
@Configuration
public class ElasticSearchConfig {

    /**
      *es集群配置路径
      */
    @Value("${study.elasticsearch.elasticList}")
    private String esHostList;


    /**
      * @Description 创建RestHighLevelClient高级客户端
      * @Author: 李炯
      * @Date: 2020/5/21 10:46
      * @Param: []
      * @Return: org.elasticsearch.client.RestHighLevelClient
      * @Exception:
      **/
    @Bean
    public RestHighLevelClient getRestHighLevelClient(){
        String[] split = esHostList.split(",");
        //创建HttpHost数组，其中存放es主机和端口的配置信息
        HttpHost[] httpHostArray = new HttpHost[split.length];
        for(int i=0;i<split.length;i++){
            String item = split[i];
            httpHostArray[i] = new HttpHost(item.split(":")[0], Integer.parseInt(item.split(":")
                    [1]), "http");
        }
        //创建RestHighLevelClient客户端
        return new RestHighLevelClient(RestClient.builder(httpHostArray));
    }



}
